import 'package:framework/data/model/domain_bean.dart';

class DisTimeUtil {
  /// 注册时间生成方法
  static List<Map<String, String>> generateRegistrationYears(
          {int? currentYear, int yearsBack = 19}) =>
      [
        {"name": "不限", "value": ""},
        for (var year = currentYear ?? DateTime.now().year;
            year >= (currentYear ?? DateTime.now().year) - yearsBack;
            year--)
          {"name": "≤$year年", "value": "≤$year"}
      ];

  /// 删除时间生成方法
  static List<DomainFilterOption>? generateDeletionDates(
      DomainFilterChild child,
      {DateTime? startDate,
      int days = 18,
      bool addLabel = true}) {
    final now = startDate ?? DateTime.now();
    List<DomainFilterOption> list = [];
    list.add(DomainFilterOption(id: 1, name: "不限", value: ""));
    for (var i = 2; i < days; i++) {
      list.add(DomainFilterOption(
          id: i,
          name: (addLabel && i < 1)
              ? "${_formatDate(now.add(Duration(days: i)))} 待删除（可预定）"
              : _formatDate(now.add(Duration(days: i))),
          value: _formatDate(now.add(Duration(days: i)))));
    }
    list.add(DomainFilterOption(
        id: list.length + 1, name: "自定义", value: "自定义", children: [child]));
    return list;
  }

  /// 获取这个月底6天的数据
  static List<Map<String, String>> generateMonthEndPairs() {
    final now = DateTime.now();
    final lastDay = DateTime(now.year, now.month + 1, 0); // 本月最后一天
    final startDate = lastDay.subtract(const Duration(days: 5)); // 从倒数第6天开始

    return List.generate(6, (i) {
      final currentDate = startDate.add(Duration(days: i)); // 连续生成6天
      return {
        "name": _formatDate(currentDate),
        "value": _formatDate(currentDate)
      };
    });
  }

  static String _formatDate(DateTime date) =>
      "${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}";
}
